const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    page: 1,
    pageSize: 20,
    trainHistoryList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const phone = wx.getStorageSync("phone");
    this.setData({
      phone
    });
    this.fetchData();
  },
  fetchData: function() {
    const page = this.data.page;
    const pageSize = this.data.pageSize;
    const phone = this.data.phone;
    const url = app.globalData.url;
    const trainHistoryList = this.data.trainHistoryList;
    const that = this;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/train/trainHistory',
      data: {
        phone,
        page,
        pageSize
      },
      method: 'POST',
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const historyList = response.data.content;
          if (historyList && historyList.length > 0) {
            historyList.forEach(function(item) {
              //完成质量
              if (item.finishQuality ==='GOOD'){
                item.finishStarCheckArr = [1,2,3,4,5];
                item.finishStarArr = [];
              } else if (item.finishQuality === 'GENERAL'){
                item.finishStarCheckArr = [1, 2, 3];
                item.finishStarArr = [1,2];
              } else if (item.finishQuality === 'BAD'){
                item.finishStarCheckArr = [1];
                item.finishStarArr = [1,2,3,4];
              }
              //训练强度
              if (item.trainStrength ==='HIGH'){
                item.strengthStarCheckArr = [1, 2, 3, 4, 5];
                item.strengthStarArr = [];
              } else if (item.trainStrength === 'MIDDLE'){
                item.strengthStarCheckArr = [1, 2, 3];
                item.strengthStarArr = [1, 2];
              } else if (item.trainStrength === 'LOW'){
                item.strengthStarCheckArr = [1];
                item.strengthStarArr = [1, 2, 3, 4];
              }
              if (item.memberFeeling ==='EASY'){
                item.memberFeeling = '轻松';
              } else if (item.memberFeeling === 'LABORIOUS'){
                item.memberFeeling = '费力';
              } else if (item.memberFeeling === 'UNCOMFORTABLE'){
                item.memberFeeling = '难受';
              }
              trainHistoryList.push(item);
            });
          }
        }
        that.setData({
          trainHistoryList
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      trainHistoryList: []
    });
    this.fetchData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const page = this.data.page;
    this.setData({
      page: page + 1
    });
    this.fetchData();
  },
})